package com.yc.mvplib.Retrofit.net;

import android.accounts.AccountsException;
import android.accounts.NetworkErrorException;
import android.app.Activity;
import android.support.annotation.NonNull;


import com.google.gson.JsonIOException;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
import com.yc.mvplib.FastConfig;
import com.yc.mvplib.R;
import com.yc.mvplib.Retrofit.HttpConfig;
import com.yc.mvplib.Retrofit.net.entity.BaseEntity;
import com.yc.mvplib.utlis.ActivityStackUtil;
import com.yc.mvplib.utlis.NetworkUtil;

import java.net.ConnectException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;


import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.internal.disposables.DisposableHelper;
import io.reactivex.internal.util.EndConsumerHelper;
import io.reactivex.observers.DefaultObserver;
import retrofit2.HttpException;



/**
 * Created by lenovo on 2017/12/4.
 * 网络回调
 */

public abstract class NetCallBack<JsonElement>  implements Observer<BaseEntity<JsonElement>> {

    @Override
    public void onSubscribe( Disposable d) {

    }
    /**
     * 完成
     */
    @Override
    public void onComplete() {

    }

    @Override
    public void onError(Throwable e) {
        int reason = R.string.fast_exception_other_error;
        int code = HttpConfig.HttpErr.EXCEPTION_OTHER_ERROR;
        Activity current = ActivityStackUtil.getInstance().getCurrent();
        if (!NetworkUtil.isConnected(current)) {
            reason = R.string.fast_exception_network_not_connected;
            code = HttpConfig.HttpErr.EXCEPTION_NETWORK_NOT_CONNECTED;
        } else {
            if (e instanceof NetworkErrorException) {//网络异常--继承于AccountsException
                reason = R.string.fast_exception_network_error;
                code = HttpConfig.HttpErr.EXCEPTION_NETWORK_ERROR;
            } else if (e instanceof AccountsException) {//账户异常
                reason = R.string.fast_exception_accounts;
                code = HttpConfig.HttpErr.EXCEPTION_ACCOUNTS;
            } else if (e instanceof ConnectException) {//连接异常--继承于SocketException
                reason = R.string.fast_exception_connect;
                code = HttpConfig.HttpErr.EXCEPTION_CONNECT;
            } else if (e instanceof SocketException) {//socket异常
                reason = R.string.fast_exception_socket;
                code = HttpConfig.HttpErr.EXCEPTION_SOCKET;
            } else if (e instanceof HttpException) {// http异常
                reason = R.string.fast_exception_http;
                code = HttpConfig.HttpErr.EXCEPTION_HTTP;
            } else if (e instanceof UnknownHostException) {//DNS错误
                reason = R.string.fast_exception_unknown_host;
                code = HttpConfig.HttpErr.EXCEPTION_UNKNOWN_HOST;
            } else if (e instanceof JsonSyntaxException
                    || e instanceof JsonIOException
                    || e instanceof JsonParseException) {//数据格式化错误
                reason = R.string.fast_exception_json_syntax;
                code = HttpConfig.HttpErr.EXCEPTION_JSON_SYNTAX;
            } else if (e instanceof SocketTimeoutException || e instanceof TimeoutException) {
                reason = R.string.fast_exception_time_out;
                code = HttpConfig.HttpErr.EXCEPTION_TIME_OUT;
            } else if (e instanceof ClassCastException) {
                reason = R.string.fast_exception_class_cast;
                code = HttpConfig.HttpErr.EXCEPTION_CLASS_CAST;
            }
        }

        _onfail(reason, code, e);
    }

    @Override
    public void onNext(BaseEntity<JsonElement> httpResult) {
        if (httpResult == null) {
            _onfail(R.string.fast_multi_error,HttpConfig.HttpErr.EXCEPTION_DATA_ERROR ,new Exception());
        }else if (httpResult.isSuccess()){
            JsonElement t= httpResult.getData();
            success(t);
        }else {
            String msg=httpResult.getMsg();
            int code=httpResult.getCode();
            onfail(msg,code,new Exception());
        }

    }
    private void _onfail(int errorRes, int errorCode, @NonNull Throwable e) {
        onfail(FastConfig.getInstance().getString(errorRes),errorCode,e);
    }

    public abstract void success(@NonNull JsonElement entity);
    public  abstract void onfail(String errorRes, int errorCode, @NonNull Throwable e);


}
